package com.wn.web;

import com.wn.auth.JWTUtil;
import com.wn.common.Const;
import com.wn.ov.FormOv;
import com.wn.ov.UserOv;
import com.wn.pojo.Appointment;
import com.wn.pojo.User;
import com.wn.resopnse.ServerResponse;

import com.wn.service.FrontService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 用户web层
 */
@RestController
@RequestMapping("patient")
public class PatientController {

    @Autowired
    private FrontService frontService;

    //登录
    @PostMapping("login")
    public ServerResponse login(@RequestBody @Validated UserOv userOv) {

        return frontService.login(userOv);

    }

    //注册
    @PostMapping("register")
    public ServerResponse register(@RequestBody @Validated User user){
         return frontService.register(user);
    }

    //预约
    @PostMapping("addAppointment")
    public ServerResponse addAppointment(@RequestBody @Validated FormOv formOv, @RequestHeader("token") String token) {
        UserOv userOv = JWTUtil.parserToken(token, Const.PUBLIC_KEY);
        int id=userOv.getId();
        Appointment appointment=new Appointment();
        appointment.setDepartmentId(formOv.getDepartmentId());
        appointment.setDoctorUserId(formOv.getDoctorUserId());
        String time="";
        time=formOv.getDate()+"-"+formOv.getTime();
        appointment.setTime(time);
        appointment.setPatientId(id);
        return frontService.addAppoint(appointment);

    }

    //预约详情
    /**
     * 预约详情信息
     * @return
     */
    @GetMapping("appointmentMsg")
    public ServerResponse appointmentMsg(@RequestHeader("token") String token){
        UserOv userOv = JWTUtil.parserToken(token, Const.PUBLIC_KEY);
        int id=userOv.getId();

        return frontService.appointmentMsg(id);

    }

    /**
     * 个人中心信息展示22
     * @param token
     * @return
     */

    @PostMapping("getUserMsgById")
    public ServerResponse getUserMsgById(@RequestHeader("token") String token){
        UserOv userOv = JWTUtil.parserToken(token, Const.PUBLIC_KEY);
        //System.out.println(userOv);
        int id=userOv.getId();
        ServerResponse userMsgById = frontService.getUserMsgById(id);
        return userMsgById;
    }
}
